RAIS  3.2
C:/Projekte/RAIS/common/FunctionRole.cs
Go to the documentation of this file.
00001 
00028 using System;
00029 using System.Collections.Generic;
00030 using System.Text;
00031 using System.Data;
00032 
00033 using FunctionRoleAttributes = System.Collections.Generic.Dictionary<string, RAIS.Common.UserManagement.FunctionRoleAttribute>;
00034 using UserTypes = System.Collections.Generic.Dictionary<string, RAIS.Common.UserManagement.UserTypeData>;
00035 using FunctionRoleValidationTypesByName = System.Collections.Generic.Dictionary<string, RAIS.Common.UserManagement.FunctionRole.ValidationType>;
00036 using FunctionRoles = System.Collections.Generic.Dictionary<string, RAIS.Common.UserManagement.FunctionRole.FunctionRoleType>;
00037 
00038 namespace RAIS.Common.UserManagement
00039 {
00043     public class FunctionRole : DBObject
00044     {
00045                 #region Enumerators
00046         public enum ValidationType
00047         {
00048             NeedsValidation = 0,
00049             Validates,
00050             DoesNotNeedValidation
00051         }
00052                 public enum FunctionRoleType
00053                 {
00054                         Administrator,
00055                         Regulator,
00056                         Licensee,
00057                         PublicBody,
00058                         Guest,
00059                         Other
00060                 }
00061                 #endregion
00062 
00063         #region Fields
00064                 static Dictionary<ValidationType, ValidationTypeData> validationTypesDictionary; 
00065                 static FunctionRoleValidationTypesByName validationTypesDictionaryByName; 
00066                 static Dictionary<FunctionRoleType, FunctionRoleTypeData> functionRoleTypesDictionary;
00067                 
00068                 bool modified = false;                                              
00069                 ValidationType validationType;                                                                          
00070         FunctionRoleAttributes attributes = new FunctionRoleAttributes();       
00071         FunctionRoleAttributes addedAttributes = new FunctionRoleAttributes();  
00072         FunctionRoleAttributes removedAttributes = new FunctionRoleAttributes();        
00073                 UserTypes userTypes = new UserTypes();
00074                 UserTypes addedUserTypes = new UserTypes();
00075                 UserTypes removedUserTypes = new UserTypes();
00076                 FunctionRoleType type;
00077         #endregion
00078 
00079                 #region Properties
00080 
00083                 static public ValidationType DefaltValidation
00084                 {
00085                         get { return ValidationType.NeedsValidation; }
00086                 }
00090                 static public FunctionRoleType DefaltType
00091                 {
00092                         get { return FunctionRoleType.Other; }
00093                 }
00099                 static public Dictionary<ValidationType, ValidationTypeData> ValidationTypesDictionary
00100                 {
00101                         get { return validationTypesDictionary; }
00102                         set { validationTypesDictionary = value; }
00103                 }
00109         static public FunctionRoleValidationTypesByName ValidationTypeNamesDictionary
00110                 {
00111                         get { return validationTypesDictionaryByName; }
00112             set { validationTypesDictionaryByName = value; }
00113                 }
00119                 public static Dictionary<FunctionRoleType, FunctionRoleTypeData> FunctionRoleTypesDictionary
00120                 {
00121                         get { return FunctionRole.functionRoleTypesDictionary; }
00122                         set { FunctionRole.functionRoleTypesDictionary = value; }
00123                 }
00124         
00128         public ValidationType Validation
00129         {
00130             get { return this.validationType; }
00131             set 
00132             { 
00133                 this.validationType = value;
00134                 this.Modified = true;
00135             }
00136         }
00140                 public ValidationTypeData ValidationData
00141                 {
00142                         get { return validationTypesDictionary[this.validationType]; }
00143                 }
00147         public FunctionRoleAttributes Attributes
00148         {
00149             get { return this.attributes; }
00150             set { this.attributes = value; }
00151         }
00155         public FunctionRoleAttributes AddedAttributes
00156         {
00157             get { return this.addedAttributes; }
00158             set { this.addedAttributes = value; }
00159         }
00163         public FunctionRoleAttributes RemovedAttributes
00164         {
00165             get { return this.removedAttributes; }
00166         }
00170                 public UserTypes AddedUserTypes
00171                 {
00172                         get { return this.addedUserTypes; }
00173                 }
00177                 public UserTypes RemovedUserTypes
00178                 {
00179                         get { return this.removedUserTypes; }
00180                 }
00184         public bool Modified
00185         {
00186             get { return this.modified; }
00187             set 
00188             {
00189                 if (!value)
00190                 {
00191                     this.RemovedAttributes.Clear();
00192                     this.AddedAttributes.Clear();
00193                                         this.RemovedUserTypes.Clear();
00194                                         this.AddedUserTypes.Clear();
00195                 }
00196                 this.modified = value; 
00197             }
00198         }
00202                 public UserTypes UserTypes
00203                 {
00204                         get { return this.userTypes; }
00205                         set     { this.userTypes = value; }
00206                 }
00210                 public FunctionRoleType Type
00211                 {
00212                         get 
00213                         {
00214                                 //FunctionRoleType type = FunctionRoleType.Other;
00215                                 //try
00216                                 //{
00217                                 //    type = (FunctionRoleType)Enum.Parse(typeof(FunctionRoleType), this.name);
00218                                 //}
00219                                 //catch {}
00220                                 //return type;
00221 
00222                                 return this.type;
00223                         }
00224                 }
00228                 public FunctionRoleTypeData TypeData
00229                 {
00230                         get { return FunctionRoleTypesDictionary[this.type]; }
00231                 }
00232                 #endregion
00233 
00234         #region Constructors
00235         public FunctionRole()
00236         {
00237                         this.validationType = DefaltValidation;
00238                 }
00239         public FunctionRole(IDataRecord record)
00240                 {
00241                         this.ID = (int)record["PK Functional Role ID"];
00242             this.Name = record["Functional Role Name"].ToString();
00243                         this.validationType = (ValidationType)Enum.Parse(typeof(ValidationType), record["Validation Type Name"].ToString());
00244                         this.type = (FunctionRoleType)Enum.Parse(typeof(FunctionRoleType), record["Functional Role Type Name"].ToString());
00245                 }
00246                 #endregion
00247 
00248         #region Public methods
00249 
00250         public bool ContainsCategoryRestriction(string catregoryName)
00251         {
00252             foreach (FunctionRoleAttribute attribute in attributes.Values)
00253             {
00254                 if (attribute.Category.Name == catregoryName)
00255                     return true;
00256             }
00257             return false;
00258         }
00259         public bool ContainsSubcategoryRestriction(FunctionRole.Category category, string subcatregoryName)
00260         {
00261             foreach (FunctionRoleAttribute attribute in attributes.Values)
00262             {
00263                 if (attribute.Category.Name == category.Name &&
00264                     attribute.Subcategory.Name == subcatregoryName)
00265                     return true;
00266             }
00267             return false;
00268         }
00269         public bool ContainsMaskRestriction(FunctionRole.Category category,
00270                                             FunctionRole.Subcategory subcategory, 
00271                                             string maskName)
00272         {
00273             foreach (FunctionRoleAttribute attribute in attributes.Values)
00274             {
00275                 if (attribute.Category.Name == category.Name &&
00276                     attribute.Subcategory.Name == subcategory.Name &&
00277                     attribute.Mask.Name == maskName)
00278                     return true;
00279             }
00280             return false;
00281         }
00282         public bool ValidateNewAttribute(FunctionRoleAttribute newAttribute, ref string returnMessage)
00283         {
00284             FunctionRoleAttributes funcitonRoleAttributes = this.Attributes;
00285             if (this.Attributes.ContainsKey(newAttribute.ToString()))
00286             {
00287                 returnMessage = RAIS.Common.Messages.FUNCTION_ROLE_ATTRIBUTE_EXISTS;
00288                 return false;
00289             }
00290             else
00291             if(this.RestricitonsConflict(newAttribute) )
00292             {
00293                 returnMessage = RAIS.Common.Messages.FUNCTION_ROLE_ATTRIBUTE_CONFLICT;
00294                 return false;
00295             }
00296             else
00297                 return true;
00298         }
00299 
00300         public bool AddAttribute(FunctionRoleAttribute newAttribute, ref string returnMessage)
00301         {
00302             bool success = this.ValidateNewAttribute(newAttribute, ref returnMessage);
00303             if (success)
00304             {
00305                 //if( this.RemovedAttributes.ContainsKey(newAttribute.ToString()))
00306                 //    this.RemovedAttributes.Remove(newAttribute.ToString());
00307                 //else
00308                     this.AddedAttributes.Add(newAttribute.ToString(), newAttribute);
00309                 
00310                 this.Attributes.Add(newAttribute.ToString(), newAttribute);
00311 
00312                 this.Modified = true;
00313             }
00314             return success;
00315         }
00316         public bool RemoveAttribute(FunctionRoleAttribute removedAttribute, ref string returnMessage)
00317         {
00318             if (this.AddedAttributes.ContainsKey(removedAttribute.ToString()))
00319                 this.AddedAttributes.Remove(removedAttribute.ToString());
00320             else
00321                 this.RemovedAttributes.Add(removedAttribute.ToString(), removedAttribute);
00322 
00323             this.Attributes.Remove(removedAttribute.ToString());
00324             this.Modified = true;
00325             return true;
00326         }
00327 
00328                 public bool AddUserType(UserTypeData newUserType, ref string returnMessage)
00329                 {
00330                         if (this.RemovedUserTypes.ContainsKey(newUserType.VisibleName))
00331                                 this.RemovedUserTypes.Remove(newUserType.VisibleName);
00332                         else
00333                                 this.AddedUserTypes.Add(newUserType.VisibleName, newUserType);
00334 
00335                         this.UserTypes.Add(newUserType.VisibleName, newUserType);
00336                         this.Modified = true;
00337                         return true;
00338                 }
00339 
00340                 public bool RemoveUserType(UserTypeData removedUserType, ref string returnMessage)
00341                 {
00342             bool success = this.ValidateRemoveUserType(removedUserType, ref returnMessage);
00343                         if (success)
00344                         {
00345                                 if (this.AddedUserTypes.ContainsKey(removedUserType.VisibleName))
00346                                         this.AddedUserTypes.Remove(removedUserType.VisibleName);
00347                                 else
00348                                         this.RemovedUserTypes.Add(removedUserType.VisibleName, removedUserType);
00349 
00350                                 this.UserTypes.Remove(removedUserType.VisibleName);
00351                                 this.Modified = true;
00352                         }
00353                         return success;
00354                 }
00355 
00356                 public FunctionRoleAttribute GetAttributeByIndex(int index)
00357                 {
00358                         FunctionRoleAttribute attribute = null;
00359 
00360                         if (this.attributes.Count >= index)
00361                         {
00362                                 int i = 0;
00363                                 foreach (KeyValuePair<string, FunctionRoleAttribute> kvp in this.attributes)
00364                                 {
00365                                         if (i == index)
00366                                         {
00367                                                 attribute = kvp.Value;
00368                                                 break;
00369                                         }
00370                                         i++;
00371                                 }
00372                         }
00373 
00374                         return attribute;
00375                 }
00376                 #endregion
00377 
00378         #region Private helper methods
00379         private bool RestricitonsConflict(FunctionRoleAttribute newAttribute)
00380         {
00381             FunctionRoleAttributes funcitonRoleAttributes = this.Attributes;
00382             foreach (FunctionRoleAttribute attribute in funcitonRoleAttributes.Values)
00383             {
00384                 if (newAttribute.RestricitonsConflict(attribute))
00385                 {
00386                     return true;
00387                 }
00388             }
00389             return false;
00390         }
00391 
00392                 private bool ValidateRemoveUserType(UserTypeData removedUserType, ref string returnMessage)
00393                 {
00394                         bool success = true;
00395                         return success;
00396                 }
00397         #endregion
00398 
00399         #region Nested classes
00400 
00403         public class Category : DBObject
00404         {
00405                     #region Constructors
00406             public Category()
00407             {}
00408             public Category(IDataRecord record)
00409                     {
00410                                 this.ID = (int)record["PK Category ID"];
00411                                 this.Name = record["Category Name"].ToString();
00412             }
00413                     #endregion
00414             #region Public methods
00415             public bool RestricitonsConflict(Category category)
00416             {
00417                 if (category.Name == RAIS.Common.Constants.ALL_CATEGORIES &&
00418                     this.Name != RAIS.Common.Constants.ALL_CATEGORIES)
00419                     return true;
00420                 else
00421                     if (this.Name == RAIS.Common.Constants.ALL_CATEGORIES &&
00422                         category.Name != RAIS.Common.Constants.ALL_CATEGORIES)
00423                         return true;
00424                     else
00425                         return false;
00426             }
00427             #endregion
00428         }
00432         public class Subcategory : DBObject
00433                 {
00434                         #region Fields
00435                         Category parent;
00436                         bool customSubcategory = true;
00437                         int order = 0;
00438                         int imageIndex = 0;
00439                         #endregion
00440                         #region Properties
00441 
00444                         public Category Parent
00445                         {
00446                                 get { return this.parent; }
00447                                 set { this.parent = value; }
00448                         }
00452                         public string FullName
00453                         {
00454                 get
00455                 {
00456                     if (this.Parent != null)
00457                         return this.Parent.Name + Constants.SEPARATOR + this.Name;
00458                     else
00459                         return this.Name;
00460                 }
00461                         }
00465                         public bool CustomSubcategory
00466                         {
00467                                 get { return customSubcategory; }
00468                                 set { customSubcategory = value; }
00469                         }
00473                         public int Order
00474                         {
00475                                 get { return order; }
00476                                 set { order = value; }
00477                         }
00481                         public int ImageIndex
00482                         {
00483                                 get { return imageIndex; }
00484                                 set { imageIndex = value; }
00485                         }
00486                         #endregion
00487                         #region Constructors
00488                         public Subcategory()
00489             { }
00490             public Subcategory(IDataRecord record)
00491             {
00492                 this.ID = (int)record["PK Subcategory ID"];
00493                 this.Name = record["Subcategory Name"].ToString();
00494                                 this.parent = new Category(record);
00495                                 this.customSubcategory = (int.Parse(record["Custom Subcategory"].ToString()) == 1);
00496                                 this.order = (int)record["Order"];
00497                         }
00498             #endregion
00499             #region Public methods
00500             public bool RestricitonsConflict(Subcategory subcategory)
00501             {
00502                 if (subcategory.Name == RAIS.Common.Constants.ALL_SUBCATEGORIES &&
00503                     this.Name != RAIS.Common.Constants.ALL_SUBCATEGORIES)
00504                     return true;
00505                 else
00506                     if (subcategory.Name != RAIS.Common.Constants.ALL_SUBCATEGORIES &&
00507                         this.Name == RAIS.Common.Constants.ALL_SUBCATEGORIES)
00508                         return true;
00509                     else
00510                         return false;
00511             }
00512             #endregion
00513         }
00517                 public class ValidationTypeData
00518                 {
00519                         #region Fields
00520                         int id;
00521                         string name;
00522                         string visibleName;
00523                         #endregion
00524 
00525                         #region Properties
00526                         public int ID
00527                         {
00528                                 get { return this.id; }
00529                         }
00530                         public ValidationType Type
00531                         {
00532                                 get { return (ValidationType)Enum.Parse(typeof(ValidationType), this.name); }
00533                         }
00534                         public string VisibleName
00535                         {
00536                                 get { return this.visibleName; }
00537                         }
00538                         #endregion
00539 
00540                         #region Constructors
00541                         public ValidationTypeData(IDataRecord record)
00542                         {
00543                                 this.id = (int)record["PK Functional Role Validation Type ID"];
00544                                 this.name = record["Validation Type Name"].ToString();
00545                                 this.visibleName = record["Validation Type Visible Name"].ToString();
00546                         }
00547                         #endregion
00548                 }
00552                 public class FunctionRoleTypeData
00553                 {
00554                         #region Fields
00555                         int id;
00556                         string name;
00557                         string visibleName;
00558                         #endregion
00559 
00560                         #region Properties
00561                         public int ID
00562                         {
00563                                 get { return this.id; }
00564                         }
00565                         public FunctionRoleType Type
00566                         {
00567                                 get { return (FunctionRoleType)Enum.Parse(typeof(FunctionRoleType), this.name); }
00568                         }
00569                         public string VisibleName
00570                         {
00571                                 get { return this.visibleName; }
00572                         }
00573                         #endregion
00574 
00575                         #region Constructors
00576                         public FunctionRoleTypeData(IDataRecord record)
00577                         {
00578                                 this.id = (int)record["PK Functional Role Type ID"];
00579                                 this.name = record["Functional Role Type Name"].ToString();
00580                                 this.visibleName = record["Functional Role Type Visible Name"].ToString();
00581                         }
00582                         #endregion
00583                 }
00584                 #endregion
00585     }
00586 }